package com.yuesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuesystem.common.R;
import com.yuesystem.dto.FeedbackFront;
import com.yuesystem.entity.Notice;
import com.yuesystem.entity.User;
import com.yuesystem.service.NoticeService;
import com.yuesystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 公告控制类.
 */

@RestController
@RequestMapping("/notices")
@CrossOrigin
@Slf4j
public class NoticeController {
  @Autowired
  NoticeService noticeService;
  
  @Autowired
  UserService userService;
  
  /**
   * 发布公告操作.
   */
  @RequestMapping("/save")
  public R<String> saveNotice(@RequestBody Notice notice) {
    Long userId = notice.getCreatedId();
    User user = userService.getUserMassageById(userId);
    String name = user.getUserName();
    notice.setCreatedUser(name);
    noticeService.addNotice(notice);
    return R.success("发布成功");
  }
  
  /**
   * 撤销操作.
   */
  @RequestMapping("/delete/{id}")
  public R<String> doReovke(@PathVariable(name = "id") Long id) {
    Notice notice = noticeService.getById(id);
    if (notice == null) {
      return R.error("未找到对应公告");
    } else {
      notice.setStatus(2);
      boolean ok = noticeService.updateById(notice);
      if (ok) {
        return R.success("撤销成功");
      } else {
        return R.error("撤销失败");
      }
    }
  }
  
  /**
   * 根据公告id获取公告数据.
   */
  @GetMapping("/getById/{id}")
  public R<Notice> getNotice(@PathVariable(name = "id") Long id) {
    Notice notice = noticeService.getById(id);
    return R.success(notice);
  }
  
  /**
   * 获取已发布数据.
   */
  @RequestMapping("/ReleaveData")
  public R<?> testData(Notice notice,
                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
    QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("status", 1);
    queryWrapper.orderByDesc("create_time");
    Page<Notice> page = new Page<Notice>(pageNo, pageSize);
    IPage<Notice> pageList = noticeService.page(page, queryWrapper);
    return R.success(pageList);
  }
  
  /**
   * 获取所有公告.
   */
  @RequestMapping("/list")
  public R<Page<Notice>> getList(int page, int pageSize) {
    FeedbackFront feedbackFront = new FeedbackFront(page, pageSize);
    Page<Notice> pageInfo = noticeService.getPage(feedbackFront);
    return R.success(pageInfo);
  }
  
}
